package demo4.mapper;


import demo4.pojo.User;
import demo4.util.DBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Mapper是数据持久层的类，作用是和数据库进行交互，完成增删改查功能
 */
@Component
public class UserMapper implements IUserMapper{
    @Autowired
    private DBUtil dbUtil;

    @Override
    public int deleteByUsername(String username) {
        try (Connection connection=dbUtil.getConnection()){
            String sql="delete from user where username=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,username);
            int rs=ps.executeUpdate();
            return rs;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return 0;
    }

    public  List<User> selectAll() {
        List<User> users=new ArrayList<>();
        try (Connection connection = dbUtil.getConnection()){
            String sql="select id,username,nickname,password,age from user";
            PreparedStatement ps=connection.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
                while (rs.next()){
                    int id=rs.getInt("id");
                    String username=rs.getString("username");
                    String nickname=rs.getString("nickname");
                    String password=rs.getString("password");
                    int age=rs.getInt("age");
                    User user=new User(id,username,password,nickname,age);
                    users.add(user);
                }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return users;
    }

    public int insert(User user) {
        try (Connection connection = dbUtil.getConnection()){
            String sql = "insert into user(username,password,nickname,age ) " +
                    "values (?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getNickname());
            ps.setInt(4, user.getAge());
            int i = ps.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public User selectByUsername(String username) {
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "select id,username,password,nickname,age " +
                    "from user " +
                    "where username=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            ResultSet rs=preparedStatement.executeQuery();
            if(rs.next()){
                User user = new User();
                        user.setId(rs.getInt("id"));
                        user.setUsername(rs.getString("username"));
                        user.setPassword(rs.getString("password"));
                        user.setNickname(rs.getString("nickname"));
                        user.setAge(rs.getInt("age"));
                        return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
            return null;
    }
    @Override
    public int updata(User user){
        try (Connection connection = dbUtil.getConnection()){
            String sql="update user set nickname=? , password=?,age=? " +
                    "where username=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,user.getNickname());
            ps.setString(2,user.getPassword());
            ps.setInt(3,user.getAge());
            ps.setString(4,user.getUsername());
            int rs=ps.executeUpdate();
            return rs;
        }catch (SQLException e){
            e.printStackTrace();
        }return 0;
    }




}
